//
//  TeacherCheck.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/3/22.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "TeacherCheck.h"
#import "DiandaoTableViewCell.h"
#import "Utils.h"

@implementation TeacherCheck
 NSDictionary *classImformations;
 UITextField*classText;
  NSArray *studentArr;
NSMutableArray *mustudentArr;

-(id)initWithFrame:(CGRect)frame Class:(NSString *)classId
{
    self=[super initWithFrame:frame];
    if (self) {
        self.classIdNum=classId;
        [self loadTableView:classId];

    }
    return self;
}
-(void)loadTableView:(NSString *)classid
{
    UILabel *label01=[[UILabel alloc]initWithFrame:CGRectMake(10, 0,(self.frame.size.width-20)*0.6, 30)];
    label01.text=@"姓名";
    label01.textAlignment=NSTextAlignmentCenter;
    label01.backgroundColor=[UIColor whiteColor];
    [self addSubview:label01];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(label01.frame.origin.x+label01.frame.size.width, label01.frame.origin.y,(self.frame.size.width-20)*0.4, 30)];
    view.backgroundColor=[UIColor whiteColor];
    xuanBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    xuanBtn.frame=CGRectMake(view.frame.size.width-80, 0, 50, 30);
    [xuanBtn setImage:[UIImage imageWithOriginalName:@"quanxuan"] forState:UIControlStateNormal];
    [xuanBtn addTarget:self action:@selector(xuanzhongAction:) forControlEvents:UIControlEventTouchUpInside];
    flag=YES;
    [view addSubview:xuanBtn];
    [self addSubview:view];
    
    _myTableView=[[UITableView alloc]initWithFrame:CGRectMake(10, label01.frame.origin.y+label01.frame.size.height+1, self.frame.size.width-20, self.frame.size.height-(label01.frame.origin.y+label01.frame.size.height+1)-100) style:UITableViewStylePlain];
    _myTableView.tableFooterView=[[UIView alloc]init];
    _myTableView.dataSource=self;
    _myTableView.delegate=self;
    [self addSubview:_myTableView];
    
    emptyView = [[EmptyStateView alloc] initWithFrame:CGRectMake(0, (SCREENHEIGH-110)/2-70, _myTableView.frame.size.width, 110) andBackImage:@"myfriend_empty" andBottomStr:@"暂无点到记录"];
    [_myTableView addSubview:emptyView];
    
    qrDianDao=[UIButton buttonWithType:UIButtonTypeCustom];
    qrDianDao.frame=CGRectMake(20, _myTableView.frame.origin.y+_myTableView.frame.size.height+10, self.frame.size.width-40, 50);
    [qrDianDao setTitle:@"确认点到" forState:UIControlStateNormal];
    [qrDianDao setTintColor:[UIColor whiteColor]];
    qrDianDao.hidden=YES;
    qrDianDao.layer.cornerRadius=5;
    qrDianDao.backgroundColor=RGBACOLOR(0, 180, 255, 1);
    [qrDianDao addTarget:self action:@selector(diaoDaoTouchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:qrDianDao];
    
    [self studentList:classid];
    
}
-(void)xuanzhongAction:(UIButton *)sender
{
    flag=!flag;
    if (flag==YES) {
     [xuanBtn setImage:[UIImage imageWithOriginalName:@"quanxuan"] forState:UIControlStateNormal];
        count=(int)mustudentArr.count;
        if (mustudentArr.count>0) {
            for ( int i=0; i<[mustudentArr count]; i++) {
                NSDictionary *dic=[mustudentArr objectAtIndex:i];
                NSMutableDictionary *mudic=[NSMutableDictionary dictionaryWithDictionary:dic];
                [mudic setObject:@"1" forKey:@"check"];
                [mustudentArr replaceObjectAtIndex:i withObject:mudic];
            }
        }

    }else
    {
    [xuanBtn setImage:[UIImage imageWithOriginalName:@"quxiaoquanxuan"] forState:UIControlStateNormal];
        count=0;
    if (mustudentArr.count>0)
    {
        for ( int i=0; i<[mustudentArr count]; i++) {
            NSDictionary *dic=[mustudentArr objectAtIndex:i];
            NSMutableDictionary *mudic=[NSMutableDictionary dictionaryWithDictionary:dic];
            [mudic setObject:@"0" forKey:@"check"];
            [mustudentArr replaceObjectAtIndex:i withObject:mudic];
        }
      }
    }
    [_myTableView reloadData];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{


}
-(void)studentList:(NSString *)sId
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"sy_rollcall_studentlist" forKey:@"method"];
    [dict setObject:sId forKey:@"classid"];
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
         studentArr=[NSArray array];
        [mustudentArr removeAllObjects];
        NSDictionary *dict = (NSDictionary *)resposeJson;
        studentArr=[dict objectForKey:@"re_data"];
        if (studentArr.count>0) {
         studentArr=[ConfirmationNsnull nullArr:studentArr];
        }
        mustudentArr=[NSMutableArray arrayWithArray:studentArr];
        if (mustudentArr.count>0) {
//            self->qrDianDao.hidden=NO;
//            [self->nodataImage removeFromSuperview];
            [self->emptyView hideEmptyImage];
            self->count=(int)mustudentArr.count;
            self->qrDianDao.hidden=NO;
           
        }else
        {
            self->qrDianDao.hidden=YES;
            [self->emptyView showEmptyImage];
//            if(!self->nodataImage)
//            {
//            self->nodataImage=[[UIImageView alloc]initWithFrame:CGRectMake((self->_myTableView.frame.size.width-95)/2, (self->_myTableView.frame.size.height-95)/2, 95, 95)];
//            }
//            self->nodataImage.image=[UIImage imageNamed:@"nodata"];
//            [self->_myTableView addSubview:self->nodataImage];
        }
        
         [self->_myTableView reloadData];
    } failure:^(NSInteger recode, NSError * error){
        
    }];

}

-(void)chooseClassTouchAction:(UIButton *)sender
{

}
-(void)diaoDaoTouchAction:(UIButton *)sender
{
    
    NSMutableArray *XIDArray=[NSMutableArray array];
    NSMutableArray *WIDArray=[NSMutableArray array];
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"sy_rollcall_add" forKey:@"method"];
    [dict setObject:self.classIdNum forKey:@"classid"];
    [dict setObject:USERID  forKey:@"teacherid"];
    for (int i=0; i<[mustudentArr count]; i++) {
        if ([[[mustudentArr objectAtIndex:i] objectForKey:@"check"] isEqualToString:@"1"]) {
            [XIDArray addObject:[[mustudentArr objectAtIndex:i] objectForKey:@"studentid"]];
        }else
        {
            [WIDArray addObject:[[mustudentArr objectAtIndex:i] objectForKey:@"studentid"]];
        }
    }
    NSString *gouxuanStr=[XIDArray componentsJoinedByString:@","];
    NSString *wgouxuanStr=[WIDArray componentsJoinedByString:@","];
    if ([XIDArray count]>0) {
        [dict setObject:gouxuanStr forKey:@"stuedentid1"];
    }else
    {
        [Utils alertViewWithMessage:@"请选择学生进行操作!"];
        return;
    }
    if ([WIDArray count]>0) {
        [dict setObject:wgouxuanStr forKey:@"stuedentid2"];
    }
    [dict setObject:@"1" forKey:@"type"];
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        NSDictionary *dict = (NSDictionary *)resposeJson;
        [Utils alertViewWithMessage:[dict objectForKey:@"re_info"]];
    } failure:^(NSInteger recode, NSError * error){
        
    }];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [studentArr count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    DiandaoTableViewCell *cell =(DiandaoTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell==nil) {
        cell =[[[NSBundle mainBundle]loadNibNamed:@"DiandaoTableViewCell" owner:nil options:nil] objectAtIndex:0];
    }
    cell.nameLabel.text=[[studentArr objectAtIndex:indexPath.row]objectForKey:@"studentname"];
    cell.gouBtn.enabled=NO;
    if ([[[mustudentArr objectAtIndex:indexPath.row]objectForKey:@"check"] isEqualToString:@"1"]) {
        [cell.gouBtn setImage:[UIImage imageWithOriginalName:@"xuanzhong2"] forState:UIControlStateNormal];
    }else
    {
         [cell.gouBtn setImage:[UIImage imageWithOriginalName:@"weixuanzhong2"] forState:UIControlStateNormal];
        
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DiandaoTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
    NSString *check=[[mustudentArr objectAtIndex:indexPath.row]objectForKey:@"check"];
    if ([check isEqualToString:@"1"]) {
        count--;
        check=@"0";
        NSDictionary *dic=[mustudentArr objectAtIndex:indexPath.row];
        NSMutableDictionary *mudic=[NSMutableDictionary dictionaryWithDictionary:dic];
        [mudic setObject:check forKey:@"check"];
        [mustudentArr replaceObjectAtIndex:indexPath.row withObject:mudic];
        check=[[mustudentArr objectAtIndex:indexPath.row]objectForKey:@"check"];
        [xuanBtn setImage:[UIImage imageWithOriginalName:@"quxiaoquanxuan"] forState:UIControlStateNormal];
        flag=NO;

        
    }else
    {
        count++;
       check=@"1";
        NSDictionary *dic=[mustudentArr objectAtIndex:indexPath.row];
        NSMutableDictionary *mudic=[NSMutableDictionary dictionaryWithDictionary:dic];
        [mudic setObject:check forKey:@"check"];
        [mustudentArr replaceObjectAtIndex:indexPath.row withObject:mudic];
        check=[[mustudentArr objectAtIndex:indexPath.row]objectForKey:@"check"];
        if (count==[mustudentArr count]) {
          [xuanBtn setImage:[UIImage imageWithOriginalName:@"quanxuan"] forState:UIControlStateNormal];
            flag=YES;
        }
    }
        if ([check isEqualToString:@"1"]) {
            [cell.gouBtn setImage:[UIImage imageWithOriginalName:@"xuanzhong2"] forState:UIControlStateNormal];
        }else
        {
            [cell.gouBtn setImage:[UIImage imageWithOriginalName:@"weixuanzhong2"] forState:UIControlStateNormal];
        }
        
    NSLog(@"mustudentArr=%@",mustudentArr);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
